// DirectShowDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DirectShow.h"
#include "DirectShowDlg.h"

#include "DXGraph.h"
#include "DXCamara.h"
#include "SampleGrabberCB.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

extern CALLBACKINFO cbInfo;
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CDirectShowDlg dialog




CDirectShowDlg::CDirectShowDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDirectShowDlg::IDD, pParent)
	, m_pDShow(NULL)
	, m_strVideoPath(_T(""))
	, m_nSliderTimer(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDirectShowDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_VIDEOWINDOW, m_VideoWindow);
	DDX_Control(pDX, IDC_SLIDER_MOVIE, m_VideoSlider);
}

BEGIN_MESSAGE_MAP(CDirectShowDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_ERASEBKGND()
	ON_BN_CLICKED(IDC_BTN_OPEN, &CDirectShowDlg::OnBnClickedBtnOpen)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_RUN, &CDirectShowDlg::OnBnClickedBtnRun)
	ON_BN_CLICKED(IDC_BTN_PAUSE, &CDirectShowDlg::OnBnClickedBtnPause)
	ON_BN_CLICKED(IDC_BTN_STOP, &CDirectShowDlg::OnBnClickedBtnStop)
	ON_BN_CLICKED(IDC_BTN_CAPTURE, &CDirectShowDlg::OnBnClickedBtnCapture)
	ON_WM_TIMER()
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BTN_CAMARA, &CDirectShowDlg::OnBnClickedBtnCamara)
END_MESSAGE_MAP()


// CDirectShowDlg message handlers

BOOL CDirectShowDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	m_VideoWindow.ModifyStyle(0,WS_CLIPCHILDREN);
	
	m_VideoSlider.SetRange(0,1000);
	m_VideoSlider.SetPos(0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDirectShowDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDirectShowDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDirectShowDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


BOOL CDirectShowDlg::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	CRect rect;
	m_VideoWindow.GetWindowRect(&rect);
	ScreenToClient(&rect);
	pDC->ExcludeClipRect(&rect);

	return CDialog::OnEraseBkgnd(pDC);
}


void CDirectShowDlg::OnBnClickedBtnOpen()
{
	// TODO: Add your control notification handler code here
	CString strFilter = "AVI File(*.avi)|*.avi|";
	strFilter += "MPEG File(*.mpeg;*.mpg)|*.mpeg;*.mpg|";
	strFilter += "ALL Files(*.*)|*.*|";

	CFileDialog fileDlg(TRUE,NULL,NULL,
		OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,strFilter,this);
	if(IDOK == fileDlg.DoModal())
	{
		m_strVideoPath = fileDlg.GetPathName();
		if(m_pDShow)
		{
			delete m_pDShow;
			m_pDShow = NULL;
		}
		m_pDShow = new CDXVideoGrabber/*new CDXGraph*/;
		m_pDShow->CreateGraph();
		m_pDShow->GetSGCB()->SetFilePath("C:\\");
		m_pDShow->GetSGCB()->SetOwner(GetSafeHwnd());
		m_pDShow->PlayFile(m_strVideoPath,m_VideoWindow.GetSafeHwnd());
		if(m_nSliderTimer == 0)
		{
			m_nSliderTimer = SetTimer(SLIDER_TIMER,100,NULL);
		}
	}
}

void CDirectShowDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	if(m_pDShow)
	{
		delete m_pDShow;
		m_pDShow = NULL;
	}

	if(cbInfo.pBuffer)
	{
		delete [] cbInfo.pBuffer;
		cbInfo.pBuffer = NULL;
	}

	CDialog::OnClose();
}

void CDirectShowDlg::OnBnClickedBtnRun()
{
	// TODO: Add your control notification handler code here
	if(m_pDShow)
	{
		m_pDShow->Run();
		if(m_nSliderTimer == 0)
		{
			m_nSliderTimer = SetTimer(m_nSliderTimer,100,NULL);
		}
	}
}

void CDirectShowDlg::OnBnClickedBtnPause()
{
	// TODO: Add your control notification handler code here
	if(m_pDShow)
		m_pDShow->Pause();
}

void CDirectShowDlg::OnBnClickedBtnStop()
{
	// TODO: Add your control notification handler code here
	if(m_pDShow)
		m_pDShow->Stop();
}

void CDirectShowDlg::OnBnClickedBtnCapture()
{
	// TODO: Add your control notification handler code here

	if(m_pDShow)
	{
		m_pDShow->GetSGCB()->SetOneShot(TRUE);
	}

// 	if(m_pDShow)
// 	{
// 		TCHAR szPath[MAX_PATH];
// 		GetModuleFileName(NULL, szPath, MAX_PATH); 
// 		CString strPath = szPath;
// 		strPath = strPath.Left(strPath.ReverseFind( '\\' ) + 1 );
// 		static int nIndex = 0;
// 		strPath.Format("%s\%d.bmp",strPath,nIndex++);
// 		m_pDShow->SnapshotBitmap(strPath);
// 	}
}

void CDirectShowDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if(m_nSliderTimer == nIDEvent && m_pDShow)
	{
		double posRatio;
		if(m_pDShow->GetCurrentPosRatio(&posRatio))
		{
			int nNewPos = posRatio*1000;
			if(m_VideoSlider.GetPos() != nNewPos)
			{
				m_VideoSlider.SetPos(nNewPos);
			}
		}
	}

	CDialog::OnTimer(nIDEvent);
}

void CDirectShowDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	if(pScrollBar->GetSafeHwnd() == m_VideoSlider.GetSafeHwnd())
	{
		if(m_pDShow)
		{
			double pos = m_VideoSlider.GetPos()/1000.0;
			m_pDShow->SetCurrentPosRatio(pos);
		}
	}
	else
	{
		CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
	}
}

void CDirectShowDlg::OnBnClickedBtnCamara()
{
	// TODO: Add your control notification handler code here
	if(m_pDShow)
	{
		delete m_pDShow;
		m_pDShow = NULL;
	}
	m_pDShow = new CDXCamara;
	m_pDShow->CreateGraph();
	m_pDShow->GetSGCB()->SetFilePath("C:\\");
	m_pDShow->GetSGCB()->SetOwner(GetSafeHwnd());
	m_pDShow->PlayFile("",m_VideoWindow.GetSafeHwnd());
}

LRESULT CDirectShowDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// Field the message posted by our SampleGrabber callback function.
	if (message == WM_CAPTURE_BITMAP)
	{
		if(m_pDShow)
		{
			if(m_pDShow->GetSGCB())
			{
				m_pDShow->GetSGCB()->SaveBitmap(cbInfo.dblSampleTime, cbInfo.pBuffer, cbInfo.lBufferSize);
			}
		}
	}

	return CDialog::WindowProc(message, wParam, lParam);
}